package www.yingdi365.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.Tricks.ViewPagerEx;

import java.util.List;
import java.util.Random;

import www.yingdi365.com.R;
import www.yingdi365.com.base.ZjbBaseNotLeftActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.customview.MySlider;
import www.yingdi365.com.model.AppMicrofacies;
import www.yingdi365.com.util.AudioRecoderUtils;

public class WeiXiangBenBFActivity extends ZjbBaseNotLeftActivity implements View.OnClickListener {

    private SliderLayout mDemoSlider;
    private AppMicrofacies microfaciesBean;
    private List<AppMicrofacies.PictureListBean> pictureList;
    private AudioRecoderUtils mAudioRecoderUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tu_pian_bf);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        init();
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        microfaciesBean = (AppMicrofacies) intent.getSerializableExtra(Constant.INTENT_KEY.MicrofaciesBean);
        pictureList = microfaciesBean.getPictureList();
    }

    @Override
    protected void findID() {
        mDemoSlider = (SliderLayout) findViewById(R.id.slider);
    }

    @Override
    protected void initViews() {
        mAudioRecoderUtils = new AudioRecoderUtils();
        for (int i = 0; i < pictureList.size(); i++) {
            MySlider mySlider = new MySlider(this);
            mySlider.image(pictureList.get(i).getUrl()+Constant.TuPianYaSuo);
            mDemoSlider.addSlider(mySlider);
        }
        mDemoSlider.setDuration(2500);
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageBack).setOnClickListener(this);
        mDemoSlider.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                String transformerName = SliderLayout.Transformer.values()[new Random().nextInt(16)].toString();
                mDemoSlider.setPresetTransformer(transformerName);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageBack:
                finish();
                break;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        new Thread(new Runnable() {
            @Override
            public void run() {
                palyVoice();
            }
        }).start();
    }

    public void palyVoice() {
        if (mAudioRecoderUtils != null) {
            mAudioRecoderUtils.stop();
        }
        mAudioRecoderUtils.playRecording(microfaciesBean.getMusic().getUrl());
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    public void stopPlay() {
        if (mAudioRecoderUtils != null) {
            mAudioRecoderUtils.stop();
        }
    }

    @Override
    protected void onDestroy() {
        stopPlay();
        mDemoSlider.stopAutoCycle();
        super.onDestroy();
    }
}
